package com.ysd.twocrm.quartz;

import com.ysd.twocrm.entity.Student;
import com.ysd.twocrm.entity.Weekbook;
import com.ysd.twocrm.mapper.StudentMapper;
import com.ysd.twocrm.mapper.WeekbookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component(value = "WeekBookQuartz")
@EnableScheduling//开启定时任务
public class WeekBookQuartz {
    @Autowired
    private WeekbookMapper weekbookMapper;
    @Autowired
    private StudentMapper studentMapper;
    /*
     * @Description 每周日0点0分0秒执行这个方法
     * 先查询全部学生的信息，然后给每个学生都添加一条周报信息
     * @param
     * @return void
     * @author liusupeng
     * @date 2020/5/17 15:43
     */
@Scheduled(cron = "0 0 0 * * 7")//秒、分、时、日、月、周
    public void addWeekboox(){
       System.out.println("我执行了");
    List<Student> students = studentMapper.selectList(null);
    for(int i=0;i<students.size();i++){
        Weekbook wb=new Weekbook(0,"暂无周报","",2,new Date(),null,dateTimeSeven(),students.get(i).getExit1(),students.get(i).getStudentName(),0,0,null);
        weekbookMapper.insert(wb);
    }

}

public Date dateTimeSeven(){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 7);//设置日期
    Date today = calendar.getTime();

return today;
}
}
